package com.mf.page;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;

public class TextChangeListener implements TextWatcher{
	//监听改变的文本框    
	private EditText editText;
	private String sReg;
	private String toReplaceString;
	private String oldInputText;
	
	public TextChangeListener(EditText editText, String reg)
	{
		this.editText = editText;
		this.sReg = reg;
		this.toReplaceString = toReplaceString;
	}
	
	 @Override
	 public void afterTextChanged(Editable arg0)
	 {
	 }
	 
	 public void onTextChanged(CharSequence ss, int start, int before, int count) { 
		 String editable = editText.getText().toString();
		 if(!editable.matches(sReg))
		 {
			 editText.setText(oldInputText);  
			 //设置新的光标所在位置    
			 editText.setSelection(oldInputText.length());  
		 }
		 else
		 {
			 oldInputText = editable;
		 }
	 }
	 @Override
	public void beforeTextChanged(java.lang.CharSequence arg0, int arg1, int arg2, int arg3)
	{
	}
	
	public String stringFilter(String inputText)
	{
		 Pattern   p   =   Pattern.compile(sReg);
		 Matcher   m   =   p.matcher(inputText);
		 
		 return m.replaceAll(toReplaceString).trim();
	}

}
